package com.yy.wga.common.util.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 公共线程池
 * @author lgw
 */
@Configuration
@EnableAsync
public class AsyncConfig {
    @Bean(name = "basePoolTaskExecutor", initMethod = "initialize", destroyMethod = "shutdown")
    public ThreadPoolTaskExecutor basePoolTaskExecutor() {
        //核心线程数、最大线程数和队列容量需要根据实际需求调整。如果任务量较大，可以适当增加线程数和队列容量。
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程数
        executor.setCorePoolSize(10);
        // 最大线程数
        executor.setMaxPoolSize(50);
        // 队列容量
        executor.setQueueCapacity(100000);
        // 线程空闲时间
        executor.setKeepAliveSeconds(60);
        // 线程名前缀
        executor.setThreadNamePrefix("BasePoolTaskExecutor-");
        // 拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 线程池等待已提交的任务完成后再关闭
        executor.setWaitForTasksToCompleteOnShutdown(true);
        return executor;
    }
}